defmodule Iocp.Router do
  @moduledoc false

  alias Iocp.Controllers.{TimeController, HistoryController}

  use Bitwise

  # 功能码 => controller模块
  @routes %{
    0x02 => TimeController,
    0x03 => HistoryController
  }

  # 对时响应
  def dispatch([_control, action, address_lo, address_hi | data], address) do
    ^address = (address_hi <<< 8) ||| address_lo
    case Map.fetch(@routes, action) do
      {:ok, controller} -> apply(controller, :handle, [address, data])
      _ -> nil
    end
  end

  def dispatch(_, _), do: nil
end
